package com.apes.fn.server.productRequire;

import com.apes.framework.api.DomainService;
import com.apes.framework.api.SpringManager;
import com.apes.framework.plugin.esb.api.SimpleRequest;
import com.apes.framework.util.MapUtil;
import com.apes.scm.pur.purchaseOrder.model.PurchaseOrder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;

/**
 * 类描述：
 *
 * @ClassName ThirdPartyOrder
 * @Description 描述
 * @Author wushilong
 * @Date 2023/1/11 20:16
 * @Version 1.0
 */
@Service
public class ThirdPartyOrder extends DomainService {

    @Autowired
    private ProductRequireRepository productRequireRepository;

    @Autowired
    private ProductRequireItemRepository productRequireItemRepository;


    public void thirdPartyUpdateRequireQty(PurchaseOrder purchaseOrder){
        if (!"santou".equals(purchaseOrder.getSourceType())) return;

        purchaseOrder.getPurchaseItems().forEach(purchaseOrderItem -> {
            String state = purchaseOrderItem.getPurchaseOrder().getState();
            double qty = 0;
            if ("sent".equals(state)) qty = purchaseOrderItem.getProductUomQty();
            if ("delete".equals(state) || "confirmed".equals(state)) {
                double newQty = Math.max(purchaseOrderItem.getDemandQty(), purchaseOrderItem.getProductUomQty());
                qty = -1 * newQty;
            }
            String requireId = purchaseOrderItem.getPurchaseOrder().getProductRequire();
            String productId = purchaseOrderItem.getProduct().getId();
            ProductRequire require = updateRequireQty(requireId, "", productId, qty, 0, 0);
            SpringManager.getBean(ProductRequireRepository.class).save(require);
        });
    }

    /**
     *
     * @param requireId 要货单号
     * @param requireItemId 要货明细号
     * @param productId 商品id
     * @param orderQty 制单数量
     * @param arriveQty 到货数量
     * @param changeAssignQty 变化分配数量
     * @return
     */
    public ProductRequire updateRequireQty(String requireId, String requireItemId, String productId, double orderQty, double arriveQty, double changeAssignQty) {
        ProductRequire require = new ProductRequire();
        if (StringUtils.hasText(requireId)) {
            require = productRequireRepository.findOne(requireId);
        } else if (StringUtils.hasText(requireItemId)) {
            ProductRequireItem productRequireItem = productRequireItemRepository.findOne(requireItemId);
            require = productRequireRepository.findOne(productRequireItem.getProductRequire().getId());
        }
        //变化数等于零不进行修改
        if ((orderQty + arriveQty + changeAssignQty) == 0) return require;

        //修改数量
        require.getProductRequireItems().forEach(requireItem -> {
            //已完成数
            double doneQty = requireItem.getQtyAssigned() + requireItem.getOrderQty() + requireItem.getQtyEnded();
            //判断修改要货单还是修改要货单的一条明细
            if (StringUtils.hasText(requireId) && requireItem.getProduct().getId().equals(productId)) updateQty(requireItem, doneQty, orderQty, arriveQty, changeAssignQty);
            else if (StringUtils.hasText(requireItemId) && requireItem.getId().equals(requireItemId)) updateQty(requireItem, doneQty, orderQty, arriveQty, changeAssignQty);
        });

        require.setLastUpdatedStamp(new Date());
        return require;
    }

    /**
     *
     * @param requireItem 要货明细
     * @param doneQty 完成数
     * @param orderQty 制单数量
     * @param arriveQty 到货数量
     * @param changeAssignQty 变化分配数量
     */
    private void updateQty(ProductRequireItem requireItem, double doneQty, double orderQty, double arriveQty, double changeAssignQty) {
        if (requireItem.getQtyRequire() <= doneQty && requireItem.getQtyArrive() >= doneQty)
            throw new RuntimeException("已满足要货分配，要货单号：" + requireItem.getProductRequire().getId() + "，商品：" + requireItem.getProduct().getId());
        if (orderQty != 0) requireItem.setOrderQty(requireItem.getOrderQty() + orderQty);
        if (arriveQty != 0) requireItem.setQtyArrive(requireItem.getQtyArrive() + arriveQty);
        if (changeAssignQty != 0) requireItem.setQtyAssigned(requireItem.getQtyAssigned() + changeAssignQty);
    }
}
